package _map

import "fmt"

// 复合map map[k]v, map[k1]map[k2]v
// map 的key是无序的，是hash的
func createMap() {
	m := map[string]string{
		"name":    "lihaowen",
		"course":  "golang",
		"site":    "qianfeng",
		"quality": "notbad",
	}
	traverseMap(m)
	getValues(m)
	deleteMap(m)
	m2 := make(map[string]int) // 空map定义 m2 == empty map

	var m3 map[string]int // m3 == nil

	fmt.Println(m, m2, m3)
}

func traverseMap(m map[string]string) {
	for k, v := range m {
		// 单取 k，v 与数组遍历一致
		fmt.Println(k, v)
	}
}

func getValues(m map[string]string) {
	courseName, ok := m["course"]
	fmt.Println(courseName, ok)
	if causeName, ok := m["cause"]; ok {
		// 不存在的key时会打印ZeroValue-这里是空串
		fmt.Println(causeName, ok)
	} else {
		fmt.Println("key does not exist")
	}
}

func deleteMap(m map[string]string) {
	name, ok := m["name"]
	fmt.Println(name, ok)
	delete(m, "name")
	name, ok = m["name"]
	fmt.Println(name, ok)
}

func main() {
	createMap()
}
